import random
import time
import requests


class SmsPlatform:
	def __init__(self, api_key, api_secret):
		self.api_key = api_key
		self.api_secret = api_secret
		self.api_url = "https://api.smsplatform.com/send"  # 替换为实际的短信API地址

	def generate_verification_code(self, length=4):
		"""生成指定长度的数字验证码"""
		return ''.join(random.choice('0123456789') for _ in range(length))

	def send_verification_code(self, phone, code):
		"""发送验证码短信"""
		try:
			# 构造请求参数
			params = {
				"api_key": self.api_key,
				"api_secret": self.api_secret,
				"phone": phone,
				"content": f"您的验证码是：{code}，有效期1分钟，请尽快使用。"
			}

			# 发送请求
			response = requests.post(self.api_url, data=params, timeout=10)
			result = response.json()

			# 根据实际API返回格式判断是否发送成功
			if result.get("status") == "success":
				return True, "短信发送成功"
			else:
				return False, result.get("message", "短信发送失败")
		except Exception as e:
			return False, f"发送短信时发生错误：{str(e)}"
